<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="com.vb.webpress.data.*"%>
<%
	boolean login = false;
	session = request.getSession(false);	
	
	if(session != null){
		
		if(session.getAttribute("account") != null)
			login = true;
		else
			session.invalidate();
		
		if(request.getParameter("action") != null
				&& request.getParameter("action").equals("logout")){
			session.invalidate();
			login = false;
		}
	}
	if(request.getParameter("action") != null
			&& request.getParameter("action").equals("login")){
		if(AccountDAO.validateAccount(request.getParameter("account")
				, request.getParameter("password"))){
			login = true;
			
			session = request.getSession(true);
			Cookie cookie = new Cookie("JSESSIONID", session.getId());
			cookie.setMaxAge(Integer.MAX_VALUE);
			response.addCookie(cookie);

			if(request.getParameter("timeout") != null)
				session.setMaxInactiveInterval(Integer.MAX_VALUE);
			else
				session.setMaxInactiveInterval(3 * 60 * 60);	
				
			session.setAttribute("account", request.getParameter("account"));
		}else{
			response.sendRedirect("/WebPress/login.jsp?status=wrong");//登录失败，跳转到登录页面，带入错误信息
		}
	}
	
	if(login){
		response.sendRedirect("/WebPress/index.jsp");//登录成功则跳转到主页
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<body>
<form action="/WebPress/index.jsp?action=login" method="post">
<table>
	<tr>
		<td>ID</td>
		<td><input type="text" name="account"></td>
	</tr>
	<tr>
		<td>PWD</td>
		<td><input type="password" name="password"></td>
	</tr>
	<tr>
		<td>Login Automatically</td>
		<td><input type="radio" name="timeout"
			value="<%=Integer.MAX_VALUE%>"></td>
	</tr>
	<tr>
		<td></td>
		<td><input type="submit" value="Login"></td>
	</tr>
</table>
<%
	if(request.getParameter("status") != null && request.getParameter("status").equals("wrong")){
%>
<p>Account or password is not correct.</p>
<%
	}
%>
</form>
</body>
</html>